package com.epam.javalab.giorgadze.task2.logic;

import com.epam.javalab.giorgadze.task2.entity.Paragraph;
import com.epam.javalab.giorgadze.task2.entity.Sentence;
import com.epam.javalab.giorgadze.task2.entity.Word;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * User: Luka
 * Date: 04.03.14
 * Time: 22:08
 */
public class TextWorker {
    private static final Logger log = LoggerFactory.getLogger(Parser.class);

    public static Paragraph switchFirstAndLastWords(String string) {
        Parser parser = new Parser();
        Paragraph paragraph = parser.parseParagraphsToSentences(string);
        for (Sentence sentence : paragraph.getSentenceList()) {
            //for (int i = sentence.getWordList().size() - 1; i >= 0; i--) {
            if (sentence.getWordList().get(sentence.getWordList().size() - 2) instanceof Word) {
                Word bufferWord = new Word(((Word) sentence.getWordList().get(sentence.getWordList().size() - 2)).getSymbolList());
                bufferWord.getSymbolList().get(0).setSymbol(bufferWord.getSymbolList().get(0).toString().toUpperCase().charAt(0));
                Word word = new Word(((Word) sentence.getWordList().get(0)).getSymbolList());
                System.out.println(word);
                if (!word.getSymbolList().isEmpty())
                    word.getSymbolList().get(0).setSymbol(word.getSymbolList().get(0).toString().toLowerCase().charAt(0));
                sentence.getWordList().add(sentence.getWordList().size()-1, word);
                sentence.getWordList().add(0, bufferWord);
                sentence.getWordList().remove(1);
                sentence.getWordList().remove(sentence.getWordList().size() - 3);
            }
        }
        log.info("First and last words switched in each sentence");
        return paragraph;
    }

}
